package com.jintian.smart.kernel.core.flow;

import com.jintian.smart.kernel.core.dto.OrgUnit;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ProcessControl implements Serializable {
	private static final long serialVersionUID = 1L;
	private String process;
	private String businessKey;
	private Boolean silence;
	@Builder.Default
	private List<ProcessControlItem> flowTos = new ArrayList<ProcessControlItem>();

	@Builder.Default
	private Map<String, Object> variables = new HashMap<>();

	@Builder.Default
	private Map<String, Object> transientVariables = new HashMap<>();

	private String remark;

	private boolean noCheckOwner;
	@Builder.Default
	private Map<String, Object> bizVariables = new HashMap<>();

	public void putBizVariables(String name, Object value) {
		this.bizVariables.put(name, value);
	}

	public void setRuleResult(List<Object> ruleResult) {
		this.bizVariables.put("ruleResult", ruleResult);
	}

	public void setStop(boolean stop) {
		this.bizVariables.put("stop", stop);
	}

	public void addRuleMsg(Object msg) {
		List<Object> list = (List) this.bizVariables.get("ruleResult");
		if (list == null) {
			list = new ArrayList<>();
			this.bizVariables.put("ruleResult", list);
		}
		list.add(msg);
	}

	public ProcessControlItem getFlowTo(String activity) {
		for (ProcessControlItem item : flowTos) {
			if (item.getActivity().equals("*") || item.getActivity().equals(activity)) {
				return item;
			}
		}
		return null;
	}

	@Data
	@Builder
	@NoArgsConstructor
	@AllArgsConstructor
	public static class ProcessControlItem implements Serializable {
		private static final long serialVersionUID = 1L;
		private String activity;
		private String activityLabel;
		@Builder.Default
		private List<OrgUnit> executorRange = new ArrayList<>();
		@Builder.Default
		private List<OrgUnit> executors = new ArrayList<>();
		@Builder.Default
		private Map<String, Object> variables = new HashMap<String, Object>();
		private boolean end;
	}

}
